package com.geeklan.smart.utils;

import com.geeklan.base.utils.MLog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

    public static class DateAndWeek {
        private long day; // 日期
        private int week; // 星期

        public DateAndWeek(long day, int week) {
            this.day = day;
            this.week = week;
        }

        public long getDay() {
            return day;
        }

        public void setDay(long day) {
            this.day = day;
        }

        public int getWeek() {
            return week;
        }

        public void setWeek(int week) {
            this.week = week;
        }
    }

    public static int dateFormatDay(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
        return Integer.parseInt(dateFormat.format(time));
    }

    public static String dateFormatDay() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(System.currentTimeMillis());
    }

    public static String dateFormatDay1(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(time);
    }

    public static String dateFormatDay2(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy,MM,dd");
        return dateFormat.format(time);
    }

    public static String dateFormatDay3(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
        return dateFormat.format(time);
    }

    public static String formatTime(String time) {
        try {
            // 定义输入和输出时间格式
            SimpleDateFormat inputFormat = new SimpleDateFormat(DATE_FORMAT);
            SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm");

            // 解析时间
            Date date = inputFormat.parse(time);
            return outputFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return ""; // 解析失败返回空字符串
        }
    }

    public static String formatTime1(String time) {
        try {
            // 定义输入和输出时间格式
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat outputFormat = new SimpleDateFormat("MM月dd日");

            // 解析时间
            Date date = inputFormat.parse(time);
            return outputFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return ""; // 解析失败返回空字符串
        }
    }

    // 获取今天及接下来几天的日期和星期
    public static List<DateAndWeek> getTodayAndNextDates(int days) {
        List<DateAndWeek> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();


        for (int i = 0; i <= days; i++) {
            long date = calendar.getTimeInMillis();
            int week = getChineseWeekday(calendar);
            result.add(new DateAndWeek(date, week));
            calendar.add(Calendar.DAY_OF_YEAR, 1); // 增加一天
        }

        return result;
    }

    // 获取中文格式的星期几
    private static int getChineseWeekday(Calendar calendar) {
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 获取星期几
        return dayOfWeek - 1; // 注意数组从0开始
    }

    public static int getYear(String birthDateString) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date birthDate = sdf.parse(birthDateString);
            Calendar birthCalendar = Calendar.getInstance();
            birthCalendar.setTime(birthDate);
            return birthCalendar.get(Calendar.YEAR);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int getMonth(String birthDateString) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date birthDate = sdf.parse(birthDateString);
            Calendar birthCalendar = Calendar.getInstance();
            birthCalendar.setTime(birthDate);
            return birthCalendar.get(Calendar.MONTH) + 1;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int getDay(String birthDateString) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date birthDate = sdf.parse(birthDateString);
            Calendar birthCalendar = Calendar.getInstance();
            birthCalendar.setTime(birthDate);
            return birthCalendar.get(Calendar.DATE);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    // 方法：根据出生日期计算年龄
    public static String calculateAge(String birthDateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            // 解析出生日期
            Date birthDate = sdf.parse(birthDateString);

            Calendar birthCalendar = Calendar.getInstance();
            birthCalendar.setTime(birthDate);

            Calendar currentDate = Calendar.getInstance();

            int years = currentDate.get(Calendar.YEAR) - birthCalendar.get(Calendar.YEAR);
            int months = currentDate.get(Calendar.MONTH) - birthCalendar.get(Calendar.MONTH);
            int days = currentDate.get(Calendar.DAY_OF_MONTH) - birthCalendar.get(Calendar.DAY_OF_MONTH);

            // 调整月份和年份
            if (days < 0) {
                months--;
            }
            if (months < 0) {
                months += 12;
                years--;
            }
            if(years == 0){
                return months + "个月";
            }
            return years + "岁" + months + "个月";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "0岁0个月";
    }


    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 判断给定时间是否超过当前时间 5 分钟
     * @param timeString 指定时间字符串（格式："yyyy-MM-dd HH:mm:ss"）
     * @return true：超过 5 分钟，false：未超过 5 分钟或解析失败
     */
    public static boolean isMoreThanFiveMinutes(String timeString) {

        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            Date givenTime = sdf.parse(timeString);
            long givenTimeMillis = givenTime.getTime();
            long currentTimeMillis = System.currentTimeMillis();

            return (currentTimeMillis - givenTimeMillis) > (5 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}

